package com.example.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class DrawView extends View {

    private Paint mPaint;
    private Handler mHandler = new Handler();
    private int width;
    private int height;
    private RectF mRectF;
    private float startAngle = 0;  //起始角度
    private float sweepAngle = 5;  //扇形的圆角
    private boolean isFirstCircle = true;

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int a = 5;
            int b = 3;
            if (isFirstCircle) {
                startAngle += a;
                sweepAngle += b;
            }else {
                startAngle += a + b;
                sweepAngle -= b;
            }
            if (sweepAngle >= 360) {
                isFirstCircle = false;
            }
            if (sweepAngle <= 0) {
                isFirstCircle = true;
            }
            postDelayed(this, 50);
            invalidate();
        }
    };

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(0xff00ff00);
        mPaint.setAntiAlias(true);

        mRectF = new RectF(0, 0, 500, 500);

        mHandler.postDelayed(runnable, 1000);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = 500;
        height = 500;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (widthMode) {
            case MeasureSpec.AT_MOST:
                if (widthSize < width) {
                    width = widthSize;
                }
                break;
            case MeasureSpec.EXACTLY:
                width = widthSize;
                break;

            case MeasureSpec.UNSPECIFIED:

                break;
        }

        switch (heightMode) {
            case MeasureSpec.AT_MOST:
                if (heightSize < height) {
                    height = heightSize;
                }
                break;
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.UNSPECIFIED:

                break;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xff0000ff);
        canvas.drawArc(mRectF, startAngle, sweepAngle, true, mPaint);
    }


}
